﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using OnlineBookstore.Models;

namespace OnlineBookstore.Controllers
{
    public class CapitalAdvancController : Controller
    {
        private BookcityEntities db = new BookcityEntities();

        // GET: CapitalAdvanc
        public ActionResult Index()
        {
            return View(db.CapitalAdvance.ToList());
        }

        // GET: CapitalAdvanc/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            CapitalAdvance capitalAdvance = db.CapitalAdvance.Find(id);
            if (capitalAdvance == null)
            {
                return HttpNotFound();
            }
            return View(capitalAdvance);
        }

        // GET: CapitalAdvanc/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: CapitalAdvanc/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性。有关
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,ShopId,Balance,ReChargeAmount")] CapitalAdvance capitalAdvance)
        {
            if (ModelState.IsValid)
            {
                db.CapitalAdvance.Add(capitalAdvance);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(capitalAdvance);
        }

        // GET: CapitalAdvanc/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            CapitalAdvance capitalAdvance = db.CapitalAdvance.Find(id);
            if (capitalAdvance == null)
            {
                return HttpNotFound();
            }
            return View(capitalAdvance);
        }

        // POST: CapitalAdvanc/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性。有关
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,ShopId,Balance,ReChargeAmount")] CapitalAdvance capitalAdvance)
        {
            if (ModelState.IsValid)
            {
                db.Entry(capitalAdvance).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(capitalAdvance);
        }



        // GET: CapitalAdvanc/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            CapitalAdvance capitalAdvance = db.CapitalAdvance.Find(id);
            if (capitalAdvance == null)
            {
                return HttpNotFound();
            }
            return View(capitalAdvance);
        }

        // POST: CapitalAdvanc/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            CapitalAdvance capitalAdvance = db.CapitalAdvance.Find(id);
            db.CapitalAdvance.Remove(capitalAdvance);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }


        [HttpGet]
        public JsonResult GetList()
        {
            var data = db.CapitalAdvance.Select(x => new {
                id = x.Id,
                shopId = x.ShopId,
                balance = x.Balance,
                reChargeAmount = x.ReChargeAmount
            }).ToList();

            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}
